﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using Lswweb.Model;
using Lswweb.Domain;
using Lswweb.Common;

namespace Lswweb.Controls
{
    public class SiteSettingsManage : SkinnedWebControl
    {
        #region 成员字段和构造方法

        private TextBox txtDomain;
        private TextBox txtSiteName;
        private TextBox txtCache;
        private TextBox txtExequatur;
        private TextBox txtMetaKeys;
        private TextBox txtMetaDescription;
        private TextBox txtCopyRight;

        private TextBox txtCompany;
        private TextBox txtPhone;
        private TextBox txtFax;
        private TextBox txtMobile;
        private TextBox txtAddress;
        private TextBox txtLinkMan;
        private TextBox tbxSiteEmail;
        private TextBox tbxMSN;
        private TextBox tbxQQ;

        private TextBox txtEmail;
        private TextBox txtSmtp;
        private TextBox txtEmailUser;
        private TextBox txtEmailPsw;

        private TextBox tbxPaymentUser;
        private TextBox tbxPartner;
        private TextBox tbxPaymentKey;
        private TextBox tbxPaypalUserName;

        private TextBox txtStatistic;
        private TextBox tbxSearch;
        private TextBox tbxExpressDeliveryExpressions;
        private TextBox tbxVipIntegral;
        private TextBox tbxLogisticsInsurance;
        private TextBox tbxDiscount;

        private Lswweb.Controls.Panel siteInfo;
        private Lswweb.Controls.Panel company;
        private Lswweb.Controls.Panel email;
        private Lswweb.Controls.Panel payment;
        private Lswweb.Controls.Panel other;
        private IButtonControl btnConfirm;

        #endregion

        #region 初始化控件

        protected override void InitializeSkin(System.Web.UI.Control skin)
        {
            siteInfo = (Lswweb.Controls.Panel)skin.FindControl("siteInfo");
            company = (Lswweb.Controls.Panel)skin.FindControl("company");
            email = (Lswweb.Controls.Panel)skin.FindControl("email");
            payment = (Lswweb.Controls.Panel)skin.FindControl("payment");
            other = (Lswweb.Controls.Panel)skin.FindControl("other");
            btnConfirm = (IButtonControl)skin.FindControl("btnConfirm");

            btnConfirm.Text = this.Strings["Update"];
            btnConfirm.Click += new EventHandler(btnConfirm_Click);

            if (siteInfo != null)
            {
                txtDomain = (TextBox)siteInfo.FindControl("txtDomain");
                txtSiteName = (TextBox)siteInfo.FindControl("txtSiteName");
                txtCache = (TextBox)siteInfo.FindControl("txtCache");
                txtExequatur = (TextBox)siteInfo.FindControl("txtExequatur");
                txtMetaKeys = (TextBox)siteInfo.FindControl("txtMetaKeys");
                txtMetaDescription = (TextBox)siteInfo.FindControl("txtMetaDescription");
                txtCopyRight = (TextBox)siteInfo.FindControl("txtCopyRight");

                txtDomain.Text = Lswweb.Domain.SiteSettings.Settings.SiteDomain;
                txtSiteName.Text = Lswweb.Domain.SiteSettings.Settings.SiteName;
                txtExequatur.Text = Lswweb.Domain.SiteSettings.Settings.SiteICP;
                txtMetaKeys.Text = Lswweb.Domain.SiteSettings.Settings.MetaKeywords;
                txtMetaDescription.Text = Lswweb.Domain.SiteSettings.Settings.MetaDescription;
                txtCopyRight.Text = Lswweb.Domain.SiteSettings.Settings.SiteCopyRight;
            }

            if (company != null)
            {
                txtCompany = (TextBox)company.FindControl("txtCompany");
                txtPhone = (TextBox)company.FindControl("txtPhone");
                txtFax = (TextBox)company.FindControl("txtFax");
                txtMobile = (TextBox)company.FindControl("txtMobile");
                txtAddress = (TextBox)company.FindControl("txtAddress");
                txtLinkMan = (TextBox)company.FindControl("txtLinkMan");
                tbxSiteEmail = (TextBox)company.FindControl("tbxSiteEmail");
                tbxQQ = (TextBox)company.FindControl("tbxQQ");
                tbxMSN = (TextBox)company.FindControl("tbxMSN");

                txtCompany.Text = Lswweb.Domain.SiteSettings.Settings.CompanyName;
                txtPhone.Text = Lswweb.Domain.SiteSettings.Settings.SitePhone;
                txtMobile.Text = Lswweb.Domain.SiteSettings.Settings.SiteMobile;
                txtFax.Text = Lswweb.Domain.SiteSettings.Settings.SiteFax;
                txtAddress.Text = Lswweb.Domain.SiteSettings.Settings.SiteAddress;
                txtLinkMan.Text = Lswweb.Domain.SiteSettings.Settings.LinkMan;
                tbxSiteEmail.Text = Lswweb.Domain.SiteSettings.Settings.SiteEmail;
                tbxQQ.Text = Lswweb.Domain.SiteSettings.Settings.SiteQQ.CombinationToString(',');
                tbxMSN.Text = Lswweb.Domain.SiteSettings.Settings.SiteMSN.CombinationToString(',');
            }

            if (email != null)
            {
                txtEmail = (TextBox)email.FindControl("txtEmail");
                txtSmtp = (TextBox)email.FindControl("txtSmtp");
                txtEmailUser = (TextBox)email.FindControl("txtEmailUser");
                txtEmailPsw = (TextBox)email.FindControl("txtEmailPsw");

                txtEmail.SetValue(this.Settings.SmtpEmail);
                txtSmtp.SetValue(this.Settings.SmtpServer);
                txtEmailUser.SetValue(this.Settings.SmtpUser);
                txtEmailPsw.Attributes.Add("value", this.Settings.SmptPsw);
            }

            if (payment != null)
            {
                //alipay
                tbxPaymentUser = (TextBox)payment.FindControl("tbxPaymentUser");
                tbxPartner = (TextBox)payment.FindControl("tbxPartner");
                tbxPaymentKey = (TextBox)payment.FindControl("tbxPaymentKey");

                tbxPaymentUser.SetValue(this.Settings.PaymentUser);
                tbxPartner.SetValue(this.Settings.Partner);
                tbxPaymentKey.SetValue(this.Settings.PaymentKey);

                //paypal
                tbxPaypalUserName = (TextBox)payment.FindControl("tbxPaypalUserName");

                tbxPaypalUserName.SetValue(this.Settings.PaymentPaypalUserName);
            }

            if (other != null)
            {
                txtStatistic = (TextBox)other.FindControl("txtStatistic");
                tbxSearch = (TextBox)other.FindControl("tbxSearch");
                tbxExpressDeliveryExpressions = (TextBox)other.FindControl("tbxExpressDeliveryExpressions");
                tbxVipIntegral = (TextBox)other.FindControl("tbxVipIntegral");
                tbxLogisticsInsurance = (TextBox)other.FindControl("tbxLogisticsInsurance");
                tbxDiscount = other.FindControl<TextBox>("tbxVipDiscount");

                txtStatistic.SetValue(this.Settings.Statistic);
                tbxSearch.SetValue(this.Settings.SearchKeys.CombinationToString(','));
                tbxExpressDeliveryExpressions.SetValue(this.Settings.ExpressDeliveryExpressions);
                tbxVipIntegral.SetValue(this.Settings.VipIntegral);
                tbxLogisticsInsurance.SetValue(this.Settings.LogisticsInsurance);
                tbxDiscount.SetValue(this.Settings.VipDiscount);
            }
        }

        void btnConfirm_Click(object sender, EventArgs e)
        {
            #region Site

            if (txtDomain != null)
                Lswweb.Domain.SiteSettings.Settings.SiteDomain = txtDomain.Text;

            if (txtSiteName != null)
                Lswweb.Domain.SiteSettings.Settings.SiteName = txtSiteName.Text;

            if (txtExequatur != null)
                Lswweb.Domain.SiteSettings.Settings.SiteICP = txtExequatur.Text;

            if (txtMetaKeys != null)
                Lswweb.Domain.SiteSettings.Settings.MetaKeywords = txtMetaKeys.Text;

            if (txtMetaDescription != null)
                Lswweb.Domain.SiteSettings.Settings.MetaDescription = txtMetaDescription.Text;

            if (txtCopyRight != null)
                Lswweb.Domain.SiteSettings.Settings.SiteCopyRight = txtCopyRight.Text;

            #endregion

            #region Email  Pop3 Smtp

            if (txtEmail != null)
                Lswweb.Domain.SiteSettings.Settings.SmtpEmail = txtEmail.Text;

            if (txtSmtp != null)
                Lswweb.Domain.SiteSettings.Settings.SmtpServer = txtSmtp.Text;

            if (txtEmailUser != null)
                Lswweb.Domain.SiteSettings.Settings.SmtpUser = txtEmailUser.Text;

            if (txtEmailPsw != null)
                Lswweb.Domain.SiteSettings.Settings.SmptPsw = txtEmailPsw.Text;

            #endregion

            #region Commpany

            if (txtCompany != null)
                Lswweb.Domain.SiteSettings.Settings.CompanyName = txtCompany.Text;

            if (txtPhone != null)
                Lswweb.Domain.SiteSettings.Settings.SitePhone = txtPhone.Text;

            if (txtMobile != null)
                Lswweb.Domain.SiteSettings.Settings.SiteMobile = txtMobile.Text;

            if (txtFax != null)
                Lswweb.Domain.SiteSettings.Settings.SiteFax = txtFax.Text;

            if (txtAddress != null)
                Lswweb.Domain.SiteSettings.Settings.SiteAddress = txtAddress.Text;

            if (txtLinkMan != null)
                Lswweb.Domain.SiteSettings.Settings.LinkMan = txtLinkMan.Text;

            if (tbxSiteEmail != null)
                Lswweb.Domain.SiteSettings.Settings.SiteEmail = tbxSiteEmail.Text.Trim();

            if (tbxQQ != null)
                Lswweb.Domain.SiteSettings.Settings.SiteQQ = tbxQQ.Text.Trim().TrimEnd(',').Split(',');

            if (tbxMSN != null)
                Lswweb.Domain.SiteSettings.Settings.SiteMSN = tbxMSN.Text.Trim().TrimEnd(',').Split(',');

            #endregion

            #region Payment

            if (tbxPaymentUser != null)
                Lswweb.Domain.SiteSettings.Settings.PaymentUser = tbxPaymentUser.Text.Trim();

            if (tbxPartner != null)
                Lswweb.Domain.SiteSettings.Settings.Partner = tbxPartner.Text.Trim();

            if (tbxPaymentKey != null)
                Lswweb.Domain.SiteSettings.Settings.PaymentKey = tbxPaymentKey.Text.Trim();


            this.Settings.PaymentPaypalUserName = this.tbxPaypalUserName.GetValue<string>();

            #endregion

            #region Other

            if (txtStatistic != null)
                Lswweb.Domain.SiteSettings.Settings.Statistic = txtStatistic.Text.Trim();

            if (tbxSearch != null)
                Lswweb.Domain.SiteSettings.Settings.SearchKeys = tbxSearch.Text.Trim().TrimEnd(',').Split(',');

            this.Settings.ExpressDeliveryExpressions = tbxExpressDeliveryExpressions.GetValue<string>();
            this.Settings.VipIntegral = tbxVipIntegral.GetValue<decimal>();
            this.Settings.LogisticsInsurance = tbxLogisticsInsurance.GetValue<decimal>();
            this.Settings.VipDiscount = tbxDiscount.GetValue<decimal>();


            #endregion

            Lswweb.Domain.SiteSettings.Settings.Save();
        }

        #endregion

        protected override string DefaultSkinFilename
        {
            get { return "SiteSettings.ascx"; }
        }
    }
}
